/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package construtora.limiar.dao;

import construtora.limiar.dao.generic.AbstractDAO;
import construtora.limiar.dao.generic.DAOEngine;
import construtora.limiar.dao.generic.IDAO;
import construtora.limiar.models.Endereco;
import construtora.limiar.models.Imovel;
import construtora.limiar.models.generic.AbstractCliente;
import construtora.limiar.models.generic.IModel;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author michel
 */
public class ImovelDAO extends AbstractDAO implements IDAO{
    
    public ImovelDAO(){
        this.table = "imovel";
    }

    @Override
    public String[][] getFields(IModel model) {
        Imovel imovel= (Imovel) model;
        String[][] fields = new String[3][2];
        fields[0][0] = "id_cliente";
        fields[0][1] = "'" + imovel.getCliente().getId().toString() + "'";
        fields[1][0] = "id_endereco";
        fields[1][1] = "'" + imovel.getEndereco().getId().toString() + "'";
        fields[2][0] = "metragem";
        fields[2][1] = "'" + imovel.getMetragem().toString() + "'";
        return fields;

    }

    @Override
    public IModel fetchOne(IModel model) {
        String where = this.getDefaultWhere(model);
        List<Imovel> list = (List<Imovel>) this.fetchAll(where, null, null, null);
        if (list != null && list.size() > 0)
            return list.get(0);
        else
            return null;
    }

    @Override
    public List<? extends IModel> fetchAll(String where, String order, Integer limit, Integer offset) {
        List<Imovel> list = new ArrayList<Imovel>();
        ResultSet rs = DAOEngine.fetchAll(this.table, where, order, limit, offset);
        try {
            while (rs.next()){
                AbstractCliente cliente = new AbstractCliente() {

                    @Override
                    public String getIdentificacao() {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }

                    @Override
                    public void setIdentificacao(String identificacao) {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }

                    @Override
                    public String getDocumento() {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }

                    @Override
                    public void setDocumento(String documento) {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }
                };
                Imovel imovel = new Imovel();
                imovel.setId(rs.getInt("id_imovel"));
                cliente.setId(rs.getInt("id_cliente"));
                imovel.setCliente(cliente);
                //imovel.setEndereco(new Endereco(rs.getInt("id_endereco")));
                Endereco endereco = new Endereco();
                endereco.setId(rs.getInt("id_endereco"));
                EnderecoDAO endDao = new EnderecoDAO();
                imovel.setEndereco((Endereco) endDao.fetchOne(endereco));
                imovel.setMetragem(rs.getDouble("metragem"));
                list.add(imovel);
            }
            return list;
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    
}
